package com.gjx.clock

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.gjx.clock.dao.RestClockSettingDao
import com.gjx.clock.entity.RestAlarmClock
import kotlin.concurrent.thread

class RestAdapter(
    private val viewModel: ClockViewModel,
    private val fragment: Fragment,
    private val restAlarmClockList: List<RestAlarmClock>,
    private var settingRestClock :RestAlarmClock
) : RecyclerView.Adapter<RestAdapter.ViewHolder>() {
    private var onBind: Boolean = true

    var enablePosition = -1

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val workingTime = view.findViewById(R.id.workingTime) as TextView
        val restTime  = view.findViewById(R.id.restTime) as TextView
        val enable = view.findViewById(R.id.enableRest) as Switch
        val clockSetting: ImageButton = view.findViewById(R.id.clockSetting)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.rest_clock_item,
            parent, false
        )
        val holder = ViewHolder(view)
        holder.enable.setOnCheckedChangeListener { _, isChecked ->

            val position = holder.adapterPosition
            val clock = restAlarmClockList[position]

            if(clock.id!=settingRestClock.id){
                if(isChecked){
                    RestClockSettingDao.setRestAlarm(clock)
                    settingRestClock=clock
                    notifyDataSetChanged()
                }
            }else{
                if(!isChecked){
                    RestClockSettingDao.setDefaultRestAlarm()
                    settingRestClock=RestClockSettingDao.getSavedClock()
                    notifyDataSetChanged()
                }
            }
        }
        holder.clockSetting.visibility = View.GONE

        view.setOnClickListener {
            val position = holder.adapterPosition
            val clock = restAlarmClockList[position]

            val contentView = LayoutInflater.from(fragment.context).inflate(R.layout.rest_clock_settings, null)
            val popWnd = PopupWindow (fragment.context)
            popWnd.contentView = contentView
            popWnd.width = ViewGroup.LayoutParams.MATCH_PARENT
            popWnd.height = ViewGroup.LayoutParams.WRAP_CONTENT
            popWnd.isFocusable=true

            val workingTimeEditText = contentView.findViewById<NumberPicker>(R.id.workingTimeSetting)
            val restTimeEditText = contentView.findViewById<NumberPicker>(R.id.restTimeSetting)
            workingTimeEditText.minValue=1
            workingTimeEditText.maxValue=180
            restTimeEditText.minValue=1
            restTimeEditText.maxValue=180
            val deleteBtn = contentView.findViewById<ImageButton>(R.id.deleteRestBtn)

            workingTimeEditText.value=clock.workingTime
            restTimeEditText.value=clock.restTime

            val saveButton = contentView.findViewById<Button>(R.id.saveRestBtn)
            val cancelButton = contentView.findViewById<Button>(R.id.cancelRestBtn)

            // 保存闹钟
            saveButton.setOnClickListener {
                clock.workingTime = workingTimeEditText.value
                clock.restTime = restTimeEditText.value
                thread {
                    viewModel.updateClock(clock)
                    fragment.activity?.runOnUiThread{
                        notifyDataSetChanged()
//                        notifyItemChanged(position)
                    }
                }
                if(holder.enable.isChecked){
                    RestClockSettingDao.setRestAlarm(clock)
                    settingRestClock=clock
                }
                popWnd.dismiss()
            }

            // 取消设置
            cancelButton.setOnClickListener {
                popWnd.dismiss()
            }

            //删除闹钟
            deleteBtn.setOnClickListener {
                thread {
                    viewModel.deleteClock(clock)
                    fragment.activity?.runOnUiThread{
//                        notifyItemRemoved(position)
                        notifyDataSetChanged()
                    }
                }
                popWnd.dismiss()
            }

            val rootView = LayoutInflater.from(fragment.activity).inflate(R.layout.activity_main,null)
            popWnd.showAtLocation(rootView, Gravity.CENTER,0,0)
        }
        return holder
    }

    internal fun refreshClocks() {
        viewModel.restClockList.clear()
        thread {
            viewModel.loadRestClocks()
            fragment.activity?.runOnUiThread{
                notifyDataSetChanged()
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        onBind = true
        val clock = restAlarmClockList[position]
        holder.workingTime.text = "${clock.workingTime}分钟"
        holder.restTime.text = "${clock.restTime}分钟"

        holder.enable.isChecked = (clock.id==settingRestClock.id)

        onBind = false
    }
    override fun getItemCount() = restAlarmClockList.size
}
